package com.chy.blog_server.modular.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoMarkDate;
import com.chy.blog_server.modular.web.entity.BoMusic;
import com.chy.blog_server.modular.web.mapper.BoMarkDateMapper;
import com.chy.blog_server.modular.web.service.IBoMarkDateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 描述：纪念日提醒表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/04/08
 */
@Service
@Transactional(readOnly = true)
public class BoMarkDateServiceImpl extends ServiceImpl<BoMarkDateMapper, BoMarkDate> implements IBoMarkDateService {

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 查询列表-全部
     * @param params
     * @return
     */
    public List<Map<String, Object>> listAll(Map<String, Object> params){
        return baseMapper.listAll(params);
    }

    /**
     * 查询总数-全部
     * @param params
     * @return
     */
    public int getCountAll(Map<String, Object> params){
        return baseMapper.getCountAll(params);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 刷新日期为往上一年
     * @param request
     */
    @Transactional
    public void refreshLastYear(HttpServletRequest request){
        List<BoMarkDate> list = getBaseMapper().selectList(new QueryWrapper<BoMarkDate>().eq("del_flag", 0));
        Calendar cal = Calendar.getInstance();
        for(BoMarkDate b: list){
            b.preUpdate(request);
            cal.setTime(b.getMarkDate());
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
            b.setMarkDate(cal.getTime());
        }
        updateBatchById(list);
    }

    /**
     * 刷新日期为往下一年
     * @param request
     */
    @Transactional
    public void refreshNextYear(HttpServletRequest request){
        List<BoMarkDate> list = getBaseMapper().selectList(new QueryWrapper<BoMarkDate>().eq("del_flag", 0));
        Calendar cal = Calendar.getInstance();
        for(BoMarkDate b: list){
            b.preUpdate(request);
            cal.setTime(b.getMarkDate());
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
            b.setMarkDate(cal.getTime());
        }
        updateBatchById(list);
    }

    /**
     * 查询已删除列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        return baseMapper.thoroughlyDelete(id);
    }

}
